import java.util.Scanner;

public class B2 {

    // 定义状态
    private enum State {
        IDLE,               // 待机状态
        READING_PASSWORD,   // 读取密码状态
        VALIDATING_PASSWORD, // 验证密码状态
        LOCKED,             // 锁定状态
        WAITING_TO_UNLOCK,  // 等待解锁状态
        SYSTEM_SELECTION    // 系统选择状态
    }

    private State currentState; // 当前状态
    private String password = "1234"; // 假设正确密码为 1234
    private StringBuilder enteredPassword; // 用户输入的密码
    private int incorrectAttempts; // 错误尝试次数
    private long lockTime; // 锁定时间
    private final int maxAttempts = 3; // 最大错误尝试次数
    private final int lockDuration = 120000; // 锁定持续时间（毫秒）

    public B2() {
        this.currentState = State.IDLE; // 初始状态为待机状态
        this.enteredPassword = new StringBuilder();
        this.incorrectAttempts = 0;
        this.lockTime = 0;
    }

    // 处理密码输入
    public void inputPassword(char key) {
        switch (currentState) {
            case IDLE:
                // 用户按下任何键后，进入读取密码状态
                currentState = State.READING_PASSWORD;
                break;

            case READING_PASSWORD:
                enteredPassword.append(key);
                if (enteredPassword.length() == 4) {
                    currentState = State.VALIDATING_PASSWORD;
                }
                break;

            case WAITING_TO_UNLOCK:
                // 等待锁定时间结束
                if (System.currentTimeMillis() - lockTime >= lockDuration) {
                    currentState = State.READING_PASSWORD;
                    enteredPassword.setLength(0); // 清空已输入的密码
                    incorrectAttempts = 0; // 重置错误次数
                }
                break;

            default:
                break;
        }
    }

    // 验证密码
    public void validatePassword() {
        if (currentState == State.VALIDATING_PASSWORD) {
            if (enteredPassword.toString().equals(password)) {
                // 密码正确，进入系统功能选择状态
                currentState = State.SYSTEM_SELECTION;
                System.out.println("Password correct! Access granted.");
            } else {
                incorrectAttempts++;
                if (incorrectAttempts >= maxAttempts) {
                    // 错误次数超过最大值，进入锁定状态
                    currentState = State.LOCKED;
                    lockTime = System.currentTimeMillis(); // 锁定时间开始
                    System.out.println("Too many incorrect attempts. The panel is now locked.");
                } else {
                    // 密码错误，继续读取密码
                    currentState = State.READING_PASSWORD;
                    System.out.println("Incorrect password. Try again.");
                }
                enteredPassword.setLength(0); // 清空输入的密码
            }
        }
    }

    // 解锁控制面板
    public void unlock() {
        if (currentState == State.LOCKED) {
            currentState = State.WAITING_TO_UNLOCK;
            lockTime = System.currentTimeMillis();
            System.out.println("Panel locked. Please wait for 120 seconds to try again.");
        }
    }

    // 模拟用户输入密码并验证
    public void simulatePasswordInput(String input) {
        for (char c : input.toCharArray()) {
            inputPassword(c);
            validatePassword();
        }
    }

    // 打印当前状态
    public void printCurrentState() {
        System.out.println("Current state: " + currentState);
    }

    // 主方法，用于测试
    public static void main(String[] args) {
        B2 controlPanel = new B2();
        Scanner scanner = new Scanner(System.in);

        // 模拟用户输入密码
        controlPanel.simulatePasswordInput("123");
        controlPanel.printCurrentState(); // 输出当前状态

        controlPanel.simulatePasswordInput("4");
        controlPanel.printCurrentState(); // 输出当前状态

        controlPanel.simulatePasswordInput("1234"); // 输入正确密码
        controlPanel.printCurrentState(); // 输出当前状态

        // 错误尝试超过3次，锁定控制面板
        controlPanel.simulatePasswordInput("1111");
        controlPanel.printCurrentState();
        controlPanel.simulatePasswordInput("2222");
        controlPanel.printCurrentState();
        controlPanel.simulatePasswordInput("3333");
        controlPanel.printCurrentState();

        // 模拟解锁
        controlPanel.unlock();
        controlPanel.printCurrentState();

        scanner.close();
    }
}
